
package com.xpo.settingskeeper;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

public class SettingsKeeperActivity extends ListActivity {

    private static final int MENU_BACKUP = Menu.FIRST + 1;

    private static final int MENU_CLEARALL = Menu.FIRST + 2;

    private static final int CTX_MENU_RESTORE = Menu.FIRST + 3;

    private static final int CTX_MENU_DELETE = Menu.FIRST + 4;

    private List<String> mBackupNameList = new ArrayList<String>();

    private boolean mBackupExist;

    private void clearAllBackups() {
        List<String> backupFileNameList = getBackupFileList();
        for (String backupFileName : backupFileNameList) {
            File backupFile = new File(getBackupPath() + "/" + backupFileName);
            backupFile.delete();
            backupFile = null;
        }
        fillBackupList();
    }

    private void restoreSettings(String backupName) {
        if (backupName == null) {
            return;
        }

        Toast.makeText(SettingsKeeperActivity.this, "Restore", Toast.LENGTH_LONG).show();
        SettingsKeeper systemKeeper = new SystemSettingsKeeper(SettingsKeeperActivity.this,
                backupName);
        if (systemKeeper != null) {
            systemKeeper.restore();
        }
        SettingsKeeper secureKeeper = new SystemSettingsKeeper(SettingsKeeperActivity.this,
                backupName);
        if (secureKeeper != null) {
            secureKeeper.restore();
        }
    }

    private void backupSettings() {
        Toast.makeText(SettingsKeeperActivity.this, "Backup", Toast.LENGTH_LONG).show();

        String dbName = Utility.genDbName();
        SettingsKeeper systemKeeper = new SystemSettingsKeeper(SettingsKeeperActivity.this, dbName);
        if (systemKeeper != null) {
            systemKeeper.backup();
        }
        SettingsKeeper secureKeeper = new SecureSettingsKeeper(SettingsKeeperActivity.this, dbName);
        if (secureKeeper != null) {
            secureKeeper.backup();
        }

        fillBackupList();
    }

    private void deleteBackup(String backupFileName) {
        File file = new File(getBackupPath() + "/" + backupFileName);
        if (file != null) {
            file.delete();

            fillBackupList();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, MENU_BACKUP, 0, R.string.opt_menu_backup);
        MenuItem restoreMenuItem = menu.add(0, MENU_CLEARALL, 0, R.string.opt_menu_clearall);
        if (restoreMenuItem != null) {
            restoreMenuItem.setEnabled(mBackupExist);
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_BACKUP:
                backupSettings();
                return true;

            case MENU_CLEARALL:
                clearAllBackups();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        menu.add(0, CTX_MENU_RESTORE, 0, R.string.ctx_menu_restore);
        menu.add(0, CTX_MENU_DELETE, 0, R.string.ctx_menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
        switch (item.getItemId()) {
            case CTX_MENU_RESTORE:
                restoreSettings(mBackupNameList.get((int)info.id));
                break;

            case CTX_MENU_DELETE:
                deleteBackup(mBackupNameList.get((int)info.id));
                break;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settingskeeper);

        fillBackupList();

        registerForContextMenu(getListView());
    }

	private TextView getTipTextView() {
		return (TextView)findViewById(R.id.tipTextView);
	}

    private void fillBackupList() {
        mBackupNameList = getBackupFileList();

        ArrayAdapter<String> backupList = new ArrayAdapter<String>(SettingsKeeperActivity.this,
                R.layout.backup_list_item, mBackupNameList);
        setListAdapter(backupList);

        updateOptionMenus();
    }

    private void updateOptionMenus() {
    }

    private List<String> getBackupFileList() {
        List<String> backupList = new ArrayList<String>();

        File home = new File(getBackupPath());
        File[] dbFileList = home.listFiles(new DbFilter());
        if (dbFileList != null && dbFileList.length > 0) {
            for (File file : home.listFiles(new DbFilter())) {
                backupList.add(file.getName());
            }
            mBackupExist = true;
        } else {
            mBackupExist = false;
        }

        updateTips();

        return backupList;
    }

	private void updateTips() {
		if (mBackupExist) {
            getTipTextView().setText(R.string.exist_backup);
        } else {
        	getTipTextView().setText(R.string.no_backup);
        }
	}

    private String getBackupPath() {
        return "/data/data/com.xpo.settingskeeper/databases/";
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    }

    class DbFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".db"));
        }
    }

}
